package com.mocoop.commom;

import java.io.File;

import android.content.Context;
import android.util.Log;

import com.aliyun.mbaas.oss.OSSClient;
import com.aliyun.mbaas.oss.callback.GetFileCallback;
import com.aliyun.mbaas.oss.callback.SaveCallback;
import com.aliyun.mbaas.oss.model.OSSException;
import com.aliyun.mbaas.oss.model.TokenGenerator;
import com.aliyun.mbaas.oss.storage.OSSFile;
import com.aliyun.mbaas.oss.util.OSSToolKit;
import com.mocoop.entity.AjaxInfo.FileProgress;
import com.mocoop.jfqc.MainActivity;

public class MyOSS {
	private final static String ACCESS_ID = "zz92n7uvR5ewRaqa";
	private final static String ACCESS_KEY = "VtcyE0eSNqa2DRcaVFFfYtrwYzpRBb";
	private final static String BUCKETNAME = "tic-qc";
	private static String path = "";
	private static MainActivity context;
	
	public static void Init(Context context, String userID) {
		MyOSS.context = (MainActivity) context;
		path = MyConstants.getPath(context, userID);
		File file = new File(path);
		if (!file.exists()) {
			file.mkdirs();
		}
		OSSClient.setApplicationContext(context);
		OSSClient.setTokenGenerator(new TokenGenerator() {
			public String generateToken(String httpMethod, String md5, String type, String date, String ossHeaders, String resource) {
				String signature = null;
				String content = httpMethod + "\n" + md5 + "\n" + type + "\n" + date + "\n" + ossHeaders + resource;
				try {
					signature = OSSToolKit.getHmacSha1Signature(content, ACCESS_KEY);
					signature = signature.trim();
				} catch (Exception e) {
					e.printStackTrace();
				}
				signature = "OSS " + ACCESS_ID + ":" + signature;
				return signature;
			}
		});
		OSSClient.setOSSHostId("oss-cn-hangzhou.aliyuncs.com");
	}

	public static void upload(final boolean isOriginal, final FileProgress fileProgress, final String fileID, String filePath, final MyOSSCallBack myOSSCallBack) {

		if(context.getAppMode()){
			if (myOSSCallBack != null)
				myOSSCallBack.onError(isOriginal, fileID, new OSSException("", new Exception()));
			return;
		}
		
		OSSFile ossFile = new OSSFile(BUCKETNAME, fileID);
		if (filePath.indexOf("file") == 0) {
			filePath = filePath.substring(7);
		}
		String type = fileID.substring(fileID.indexOf(".") + 1);
		ossFile.setUploadFilePath(filePath, type);
		ossFile.enableUploadCheckMd5sum();
		ossFile.uploadInBackground(new SaveCallback() {

			@Override
			public void onProgress(int currentProgress, int totalProgress) {


				if (isOriginal) {
						fileProgress.setOriginTotalProgress(totalProgress);
						fileProgress.setOriginCurrentProgress(currentProgress);
				} else {
						fileProgress.setThumbnailCurrentProgress(currentProgress);
						fileProgress.setThumbnailTotalProgress(totalProgress);
				}
				if (myOSSCallBack != null)
					myOSSCallBack.onProgress(fileProgress);
			}

			@Override
			public void onFailure(OSSException err) {
				err.printStackTrace();
				if (myOSSCallBack != null)
					myOSSCallBack.onError(isOriginal, fileID, err);
			}

			@Override
			public void onSuccess(String arg0) {
				if (myOSSCallBack != null)
					myOSSCallBack.onSuccess(isOriginal, fileID, fileProgress.getProgressID());
			}
		});

	}

	public static void download(final String fileName, final MyOSSCallBack myOSSCallBack) {

		if(context.getAppMode()){
			if (myOSSCallBack != null){
				myOSSCallBack.onError(false, fileName,  new OSSException("", new Exception()));
			}
			return;
		}
		
		OSSFile ossFile = new OSSFile(BUCKETNAME, fileName);

		try {
			ossFile.downloadToInBackground(path + fileName, new GetFileCallback() {

				@Override
				public void onProgress(int arg0, int arg1) {
					// Log.d("RZZ",fileName+"----------"+arg0+"------"+arg1);
				}

				@Override
				public void onFailure(OSSException err) {
					if (myOSSCallBack != null)
						myOSSCallBack.onError(false, fileName, err);
				}

				@Override
				public void onSuccess(String arg0, String arg1) {
					if (myOSSCallBack != null)
						myOSSCallBack.onSuccess(false, path + fileName, fileName);
				}
			});
		} catch (Exception e) {
			Log.d("RZZ", e.toString());
		}
	}

	public interface MyOSSCallBack {
		public void onSuccess(boolean isOriginal, String fileID, Object obj);

		public void onError(boolean isOriginal, String fileID, Object obj);

		public void onProgress(FileProgress fileProgress);
	}

}
